package tech.aistar.corejava.day12.dayhomeork;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2019/7/30.
 */
public class ArrayList02 {
    public static void main(String[] args) {
        List<Product> products = new ArrayList<>();

        Product p1 = new Product(1, "小米", 10.00, "河南");
        Product p2 = new Product(2, "大米", 05.00, "河南");
        Product p3 = new Product(3, "绿豆", 20.00, "河南");
        Product p4 = new Product(4, "豆芽", 19.00, "河南");
        Product p5 = new Product(5, "阿幽", 30.00, "河南");

        products.add(p1);
        products.add(p2);
        products.add(p3);
        products.add(p4);
        products.add(p5);

        System.out.println(products);

        System.out.println("=================");

       System.out.println(products.get(2));
       System.out.println("=================");
       delByProduct(products, 2);
       System.out.println(products);
       System.out.println("==================");


        Iterator<Product> iter = products.iterator();


        while (iter.hasNext()) {

            Product e = iter.next();
           System.out.println(e);

        }
        products.get(2).setPrice(products.get(2).getPrice()*1.5);
        System.out.println(products.get(2));
        System.out.println("==================");
        for (Product str: products){
            System.out.println(str);

        }
    }

    private static void delByProduct(List<Product> products, Integer id) {
        if (null == products && products.size() == 0) {
            return;
        }
        for (int i = 0; i < products.size(); i++) {
            if (products.get(i).getId() == (id)) {

                products.remove(products.get(i));
                break;
            }
        }
    }

}
